<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    从流开始的IO世界生活(一) |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-从流开始的IO世界生活-一" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  从流开始的IO世界生活(一)
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/08/05/%E4%BB%8E%E6%B5%81%E5%BC%80%E5%A7%8B%E7%9A%84IO%E4%B8%96%E7%95%8C%E7%94%9F%E6%B4%BB-%E4%B8%80/" class="article-date">
  <time datetime="2020-08-05T14:11:02.000Z" itemprop="datePublished">2020-08-05</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java/">Java</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.1k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">8分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="一、File类"><a href="#一、File类" class="headerlink" title="一、File类"></a>一、File类</h2><p>File：是文件和目录路径名的抽象表示</p>
<ul>
<li>文件和目录是可以通过File封装成对象的</li>
<li>对于File而言，其封装的并不是一个真正的文件，仅仅是一个路径而已。它可以是存在的，也可以是不存在的。<strong>将来是要通过具体的操作把这个路径的内容转换为具体存在的</strong>。</li>
</ul>
<h4 id="常用构造方法"><a href="#常用构造方法" class="headerlink" title="常用构造方法"></a>常用构造方法</h4><table>
<thead>
<tr>
<th>方法名</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>File(String pathname)</td>
<td>通过将给定的路径名字符串转换为抽象路径名来创建新的File实例</td>
</tr>
<tr>
<td>File(String parent,String child)</td>
<td>从父类路径名字符串和子路径名字符串创建新的File实例</td>
</tr>
<tr>
<td>File(File parent,String child)</td>
<td>从父抽象路径名和子路径名字符串创建新的File实例</td>
</tr>
</tbody></table>
<a id="more"></a>

<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-02_20-58-27.jpg" alt=""></p>
<h2 id="二、File类创建功能"><a href="#二、File类创建功能" class="headerlink" title="二、File类创建功能"></a>二、File类创建功能</h2><table>
<thead>
<tr>
<th>方法名</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>public boolean createNewFile()</td>
<td>当具有该名称的文件不存在时，创建一个由该抽象路径命名的新空文件</td>
</tr>
<tr>
<td>public boolean mkdir()</td>
<td>创建由此抽象路径名命名的目录</td>
</tr>
<tr>
<td>public boolean mkdirs()</td>
<td>创建由此抽象路径名命名的目录，包括任何必需但不存在的父目录</td>
</tr>
</tbody></table>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-02_21-20-27.jpg" alt=""></p>
<h2 id="三、File类判断和获取功能"><a href="#三、File类判断和获取功能" class="headerlink" title="三、File类判断和获取功能"></a>三、File类判断和获取功能</h2><table>
<thead>
<tr>
<th>方法名</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>public boolean isDirectory()</td>
<td>测试此抽象路径名的File是否为目录</td>
</tr>
<tr>
<td>public boolean isFile()</td>
<td>是否为文件</td>
</tr>
<tr>
<td>public boolean exists()</td>
<td>测试此抽象路径名表示的File是否存在</td>
</tr>
<tr>
<td>public String getAbsolutePath()</td>
<td>返回此抽象路径名的绝对路径名字符串</td>
</tr>
<tr>
<td>public String getPath()</td>
<td>返回此路径名转换为路径名字符串</td>
</tr>
<tr>
<td>public String getName()</td>
<td>返回由此抽象路径名表示的文件或目录的名称</td>
</tr>
<tr>
<td>public String[] list()</td>
<td>返回此抽象路径名表示的文件或目录的名称字符串数组</td>
</tr>
<tr>
<td>public File[] listFiles()</td>
<td>返回此抽象路径名表示的目录中的文件和目录的File对象数组</td>
</tr>
</tbody></table>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-05_20-37-54.jpg" alt=""></p>
<h2 id="四、File类删除功能"><a href="#四、File类删除功能" class="headerlink" title="四、File类删除功能"></a>四、File类删除功能</h2><table>
<thead>
<tr>
<th>方法名</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>public boolean delete</td>
<td>删除由此抽象路径表示的文件或目录</td>
</tr>
</tbody></table>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> IO_;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">delete</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		File f1=<span class="keyword">new</span> File(<span class="string">"D:/IO/yolin.txt"</span>);</span><br><span class="line"><span class="comment">//		System.out.println(f1.createNewFile());</span></span><br><span class="line">		System.out.println(f1.delete());</span><br><span class="line">		<span class="comment">//删除文件同理</span></span><br><span class="line">		<span class="comment">//当删除包含文件的目录时，需要先删除目录下的文件，才能接着删除母鹿</span></span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、递归"><a href="#五、递归" class="headerlink" title="五、递归"></a>五、递归</h2><p>从编程角度来看，递归指的是方法定义中调用方法本身的现象</p>
<h4 id="递归解决问题的思路："><a href="#递归解决问题的思路：" class="headerlink" title="递归解决问题的思路："></a>递归解决问题的思路：</h4><p>把一个复杂的问题层层转化为一个<strong>与原问题相似的规模较小</strong>的问题来求解。递归策略只需要<strong>少量的程序</strong>就可描述出解题过程所需要的多次重复计算</p>
<h4 id="递归解决问题要找到两个内容："><a href="#递归解决问题要找到两个内容：" class="headerlink" title="递归解决问题要找到两个内容："></a>递归解决问题要找到两个内容：</h4><ul>
<li>递归出口：否则会出现内存溢出</li>
<li>递归规则：与原问题相似的规模较小的问题</li>
</ul>
<h3 id="案例："><a href="#案例：" class="headerlink" title="案例："></a>案例：</h3><h4 id="①用递归思想计算5的阶乘"><a href="#①用递归思想计算5的阶乘" class="headerlink" title="①用递归思想计算5的阶乘"></a>①用递归思想计算5的阶乘</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> IO_;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">digui_2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		System.out.println(f(<span class="number">5</span>));  <span class="comment">//120</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">f</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(n==<span class="number">1</span>)&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			<span class="keyword">int</span> num=n*f(n-<span class="number">1</span>);</span><br><span class="line">			<span class="keyword">return</span> num;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="②遍历目录"><a href="#②遍历目录" class="headerlink" title="②遍历目录"></a>②遍历目录</h4><p>需求：给定一个路径，请通过递归完成遍历该目录下的所有内容，并把所有文件夹的绝对路径输出在控制台</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-03_19-32-43.jpg" alt=""></p>
<h2 id="六、IO流概述与分类"><a href="#六、IO流概述与分类" class="headerlink" title="六、IO流概述与分类"></a>六、IO流概述与分类</h2><h3 id="IO流概述"><a href="#IO流概述" class="headerlink" title="IO流概述"></a>IO流概述</h3><ul>
<li>IO：输入/输出(Input/Output)</li>
<li>流：是一种抽象概念，是对数据传输的总称。也就是说数据在设备间的传输称为流，流的本质是数据传输</li>
<li>IO流就是用来处理设备间数据传输问题的，常见应用：文件复制；文件上传与下载</li>
</ul>
<h3 id="IO流分类"><a href="#IO流分类" class="headerlink" title="IO流分类"></a>IO流分类</h3><h4 id="按照数据的流向分："><a href="#按照数据的流向分：" class="headerlink" title="按照数据的流向分："></a>按照数据的流向分：</h4><ul>
<li>输入流：读数据</li>
<li>输出流：写数据</li>
</ul>
<h4 id="按照数据类型来分（常用）："><a href="#按照数据类型来分（常用）：" class="headerlink" title="按照数据类型来分（常用）："></a>按照数据类型来分（常用）：</h4><ul>
<li>字节流 （字节输入流；字节输出流）</li>
<li>字符流 （字符输入流；字符输出流）</li>
</ul>
<h2 id="七、字节流概述"><a href="#七、字节流概述" class="headerlink" title="七、字节流概述"></a>七、字节流概述</h2><h3 id="字节流抽象基类："><a href="#字节流抽象基类：" class="headerlink" title="字节流抽象基类："></a>字节流抽象基类：</h3><ul>
<li><strong>InputStream</strong>:这个抽象类是表示字节输入流的所有类的超类</li>
<li><strong>OutputStream</strong>:这个抽象类是表示字节输出流的所有类的超类</li>
<li>子类名称都是以其父类名作为后缀的</li>
</ul>
<h3 id="字节流写数据"><a href="#字节流写数据" class="headerlink" title="字节流写数据"></a>字节流写数据</h3><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>FileOutputStream</td>
<td>创建文件输出流以指定的名称写入文件</td>
</tr>
</tbody></table>
<h4 id="使用字节输出流写数据的步骤："><a href="#使用字节输出流写数据的步骤：" class="headerlink" title="使用字节输出流写数据的步骤："></a>使用字节输出流写数据的步骤：</h4><ol>
<li>创建字节输出对象<br>（调用系统功能创建文件，创建字节输出流对象，让字节输出流对象指向文件）</li>
<li>调用字节输出流对象的写数据方法</li>
<li>释放资源（关闭此文件输出流并释放与此流相关联的任何系统资源）</li>
</ol>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-03_21-05-00.jpg" alt=""></p>
<h4 id="字节流写数据的3种方式"><a href="#字节流写数据的3种方式" class="headerlink" title="字节流写数据的3种方式"></a>字节流写数据的3种方式</h4><table>
<thead>
<tr>
<th>方法名</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>void write(int b)</td>
<td>将指定的字节写入此文件输出流一次写一个字节数据</td>
</tr>
<tr>
<td>void write(byte[] b)</td>
<td>将b.length字节从指定的字节数组写入此文件输出流，一次写一个字节数组数据</td>
</tr>
<tr>
<td>void write(byte[] b,int off,int len)</td>
<td>将len字节从指定的字节数组开始，从偏移量off开始写入此文件输出流，一次写一个字节数组的部分数据</td>
</tr>
</tbody></table>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-04_11-22-58.jpg" alt=""></p>
<p>emmm~记得最后要释放资源</p>
<h4 id="字节流写数据的问题"><a href="#字节流写数据的问题" class="headerlink" title="字节流写数据的问题"></a>字节流写数据的问题</h4><p><strong>① 如何实现换行</strong>？</p>
<p> 不同的操作系统有不同的换行符：</p>
<ul>
<li>windows：\r\n</li>
<li>linux:\n</li>
<li>mac:\r</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-04_12-28-14.jpg" alt=""></p>
<p><strong>②如何追加写入？</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">FileOutputStream</span><span class="params">(String name,<span class="keyword">boolean</span> append)</span></span></span><br></pre></td></tr></table></figure>
<ul>
<li>创建文件输出流以指定的名称写入文件。如果第二个参数为true，则字节将写入文件的末尾而不是开头</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-04_13-08-43.jpg" alt=""></p>
<h3 id="字节流读取数据（一次读一个字节数据）"><a href="#字节流读取数据（一次读一个字节数据）" class="headerlink" title="字节流读取数据（一次读一个字节数据）"></a>字节流读取数据（一次读一个字节数据）</h3><h4 id="使用字节输入流读数据的步骤："><a href="#使用字节输入流读数据的步骤：" class="headerlink" title="使用字节输入流读数据的步骤："></a>使用字节输入流读数据的步骤：</h4><ol>
<li>创建字节输入流对象</li>
<li>调用字节输入流对象的读取数据方法</li>
<li>释放资源</li>
</ol>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-04_21-19-00.jpg" alt=""></p>
<h3 id="案例：复制文本文件"><a href="#案例：复制文本文件" class="headerlink" title="案例：复制文本文件"></a>案例：复制文本文件</h3><p>需求：把”D:/IO/yolin.txt”赋值到JavaSE目录下</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-04_21-37-05.jpg" alt=""></p>
<h3 id="字节流读取数据（一次读一个字节数组数据）"><a href="#字节流读取数据（一次读一个字节数组数据）" class="headerlink" title="字节流读取数据（一次读一个字节数组数据）"></a>字节流读取数据（一次读一个字节数组数据）</h3><p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-05_09-23-30.jpg" alt=""></p>
<h4 id="案例：复制图片"><a href="#案例：复制图片" class="headerlink" title="案例：复制图片"></a>案例：复制图片</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> zi_jie_liu;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">photo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException</span>&#123;</span><br><span class="line">		FileInputStream fis=<span class="keyword">new</span> FileInputStream(<span class="string">"D:/IO/mn.jpg"</span>);</span><br><span class="line">		FileOutputStream fos=<span class="keyword">new</span> FileOutputStream(<span class="string">"mn.jpg"</span>);</span><br><span class="line">		<span class="keyword">byte</span>[] byt=<span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">		<span class="keyword">int</span> len;</span><br><span class="line">		<span class="keyword">while</span>((len=fis.read(byt))!=-<span class="number">1</span>)&#123;</span><br><span class="line">		    fos.write(byt,<span class="number">0</span>,len);</span><br><span class="line">		&#125;</span><br><span class="line">		fis.close();</span><br><span class="line">		fos.close();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="字节缓冲流（提高读写效率）"><a href="#字节缓冲流（提高读写效率）" class="headerlink" title="字节缓冲流（提高读写效率）"></a>字节缓冲流（提高读写效率）</h3><ul>
<li><strong>BufferOutputStream</strong>:该类实现缓冲输出流。通过设置这样的输出流，应用程序可以向底层输出流写入字节，而不必为写入的每个字节导致底层系统的调用</li>
<li><strong>BufferedInputStream</strong>:创建BufferedInputStream将创建一个内部缓冲区数组。当从流中读取或跳过字节时，内部缓冲区将根据需要从所包含的输入流中重新填充，一次很多字节</li>
</ul>
<h4 id="构造方法"><a href="#构造方法" class="headerlink" title="构造方法"></a>构造方法</h4><ul>
<li>字节缓冲输出流：<strong>BufferedOutputStream(OutputStream out)</strong></li>
<li>字节缓冲输入流：<strong>BufferedInputStream(InputStream in)</strong></li>
</ul>
<p>为什么构造方法需要的是字节流，而不是具体的文件或路径？</p>
<ul>
<li>字节缓冲流<strong>仅仅提供缓冲区</strong>，而真正的读写数据还得依靠基本的字节流对象进行操作。</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-05_10-59-56.jpg" alt=""></p>
<h4 id="四种方式效率比较"><a href="#四种方式效率比较" class="headerlink" title="四种方式效率比较"></a>四种方式效率比较</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> zi_jie_liu;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.BufferedInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.BufferedOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">video</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;	</span><br><span class="line">		<span class="keyword">long</span> start=System.currentTimeMillis();</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		one_byte();       //59020ms左右</span></span><br><span class="line"><span class="comment">//		arr_byte();       //75ms左右</span></span><br><span class="line">		Buffer_one_test();<span class="comment">//137ms左右</span></span><br><span class="line"><span class="comment">//		Buffer_arr_test();//38ms左右</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">long</span> end=System.currentTimeMillis();</span><br><span class="line">		System.out.println(end-start+<span class="string">"毫秒"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//基本字节流一次读一个字节</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">one_byte</span><span class="params">()</span> <span class="keyword">throws</span> IOException</span>&#123;</span><br><span class="line">		FileInputStream fis=<span class="keyword">new</span> FileInputStream(<span class="string">"D:/IO/video.mp4"</span>);</span><br><span class="line">		FileOutputStream fos=<span class="keyword">new</span> FileOutputStream(<span class="string">"video2.mp4"</span>);</span><br><span class="line">		<span class="keyword">int</span> byt;</span><br><span class="line">		<span class="keyword">while</span>((byt=fis.read())!=-<span class="number">1</span>)&#123;</span><br><span class="line">			fos.write(byt);</span><br><span class="line">		&#125;</span><br><span class="line">		fis.close();</span><br><span class="line">		fos.close();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//基本字节流一次读一个字节数组</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">arr_byte</span><span class="params">()</span> <span class="keyword">throws</span> IOException</span>&#123;</span><br><span class="line">		FileInputStream fis=<span class="keyword">new</span> FileInputStream(<span class="string">"D:/IO/video.mp4"</span>);</span><br><span class="line">		FileOutputStream fos=<span class="keyword">new</span> FileOutputStream(<span class="string">"video3.mp4"</span>);</span><br><span class="line">		<span class="keyword">byte</span>[] byt=<span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">		<span class="keyword">int</span> len;</span><br><span class="line">		<span class="keyword">while</span>((len=fis.read(byt))!=-<span class="number">1</span>)&#123;</span><br><span class="line">			fos.write(byt,<span class="number">0</span>,len);</span><br><span class="line">		&#125;</span><br><span class="line">		fis.close();</span><br><span class="line">		fos.close();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//缓冲流一次读一个字节流</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Buffer_one_test</span><span class="params">()</span> <span class="keyword">throws</span> IOException</span>&#123;</span><br><span class="line">		BufferedInputStream bis=<span class="keyword">new</span> BufferedInputStream(<span class="keyword">new</span> FileInputStream(<span class="string">"D:/IO/video.mp4"</span>));</span><br><span class="line">		BufferedOutputStream bos=<span class="keyword">new</span> BufferedOutputStream(<span class="keyword">new</span> FileOutputStream(<span class="string">"video5.mp4"</span>));</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">int</span> by;</span><br><span class="line">		<span class="keyword">while</span>((by=bis.read())!=-<span class="number">1</span>)&#123;</span><br><span class="line">			bos.write(by);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//缓冲流</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Buffer_arr_test</span><span class="params">()</span> <span class="keyword">throws</span> IOException</span>&#123;</span><br><span class="line">		BufferedOutputStream bos=<span class="keyword">new</span> BufferedOutputStream(<span class="keyword">new</span> FileOutputStream(<span class="string">"video1.mp4"</span>));</span><br><span class="line">		BufferedInputStream bis=<span class="keyword">new</span> BufferedInputStream(<span class="keyword">new</span> FileInputStream(<span class="string">"D:/IO/video.mp4"</span>));</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">byte</span>[] byt=<span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">		<span class="keyword">int</span> by;</span><br><span class="line">		<span class="keyword">while</span>((by=bis.read(byt))!=-<span class="number">1</span>)&#123;</span><br><span class="line">			bos.write(byt,<span class="number">0</span>,by);</span><br><span class="line">		&#125;</span><br><span class="line">		bis.close();</span><br><span class="line">		bos.close();</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/File%E7%B1%BB/" rel="tag">File类</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%AD%97%E8%8A%82%E6%B5%81/" rel="tag">字节流</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%80%92%E5%BD%92/" rel="tag">递归</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/08/07/%E4%BB%8E%E6%B5%81%E5%BC%80%E5%A7%8B%E7%9A%84IO%E4%B8%96%E7%95%8C%E7%94%9F%E6%B4%BB-%E4%BA%8C/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            从流开始的IO世界生活(二)
          
        </div>
      </a>
    
    
      <a href="/2020/07/30/%E9%9B%86%E5%90%88%E8%BF%9B%E9%98%B6-%E5%9B%9B-Map%E9%9B%86%E5%90%88-%E9%9B%86%E5%90%88%E5%B5%8C%E5%A5%97/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">集合进阶(四)-(Map集合)(集合嵌套)(Collections)</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>